# coding=utf8
from djangomako.shortcuts import render_to_response, render_to_string
from valkyrie.models import subject_search, Subject, DownloadItem
from valkyrie.models.utils import filter_query_input

def home(request):
    return render_to_response('home.html', {})

def search(request):
    q = request.GET.get('q', '')
    q = filter_query_input(q)
    total_num = 0
    rs = []
    if q:
        total_num, rs = subject_search(q)
    return render_to_response('search_result.html', {
        'subjects': rs,
        'total_num': total_num,
        'q': q,
    })

def subject(request, sid):
    subject = Subject.objects.get(id=sid)
    downloads = DownloadItem.objects.filter(subject_id=sid)
    return render_to_response('subject.html', {
        'subject': subject,
         'downloads': downloads,
    })
